Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Data


Namespace sk.DAL
    Public MustInherit Class CommentProvider
        Inherits DataAccess
        Sub New()
            Me.ConnectionString = Globals.Settings.Comment.ConnectionString
        End Sub
        Private Shared _instance As CommentProvider = Nothing
        Public Shared ReadOnly Property Instance() As CommentProvider
            Get
                If IsNothing(_instance) Then
                    _instance = CType(Activator.CreateInstance(Type.GetType(Globals.Settings.Comment.ProviderType)), CommentProvider)
                End If

                Return _instance
            End Get
        End Property
        Public MustOverride Function GetAllComments() As Generic.List(Of Comment)
        Public MustOverride Function CreateComment(ByVal Cmt As Comment) As Boolean
        Public MustOverride Function DeleteComment(ByVal CmtID As Integer) As Boolean
        Public MustOverride Function UpdateComment(ByVal Cmt As Comment) As Boolean
        Public MustOverride Function GetCommentByID(ByVal CmtID As Integer) As Comment
        Public MustOverride Function GetCommentsForArticle(ByVal ArticleID As Integer) As Generic.List(Of Comment)
        Public MustOverride Function GetApprovedCommentsForArticle(ByVal ArticleID As Integer) As Generic.List(Of Comment)
        Public MustOverride Function GetUserCommentsForArticle(ByVal userID As String, ByVal ArticleID As Integer) As Generic.List(Of Comment)
        Public MustOverride Function GetCommentsByUserID(ByVal UserID As String) As Generic.List(Of Comment)
        Public MustOverride Function GetCommentsOnDate(ByVal CommentDate As Date) As Generic.List(Of Comment)
        Public MustOverride Function ChangeCommentApproval(ByVal CommentID As Integer, ByVal status As Boolean) As Boolean
        Public MustOverride Function GetNextID() As Integer
        Protected Overridable Function GetCommentFromReader(ByVal reader As IDataReader) As Comment

            Dim comm As New Comment

            With comm
                If IsDBNull(reader("CommentID")) Then .CommentID = 0 Else .CommentID = CInt(reader("CommentID"))
                If IsDBNull(reader("UserID")) Then .UserID = "" Else .UserID = reader("UserID").ToString
                If IsDBNull(reader("Date")) Then .CommDate = New Date(1900, 1, 1) Else .CommDate = CDate(reader("Date"))
                If IsDBNull(reader("Approved")) Then .Approved = True Else .Approved = CType(reader("Approved"), Boolean)
                If IsDBNull(reader("ArticleID")) Then .ArticleID = 0 Else .ArticleID = CInt(reader("ArticleID"))
                If IsDBNull(reader("Text")) Then .Text = "" Else .Text = reader("Text").ToString
            End With
            Return comm
        End Function

        Protected Overridable Function GetCommentCollectionFromReader( _
                ByVal reader As IDataReader) _
                As List(Of Comment)
            Dim CommentList As New List(Of Comment)
            Try
                While (reader.Read)
                    CommentList.Add(GetCommentFromReader(reader))
                End While
            Catch ex As Exception
                Throw New Exception("from GetCommentCollectionFromReader", ex)
            End Try
            Return CommentList
        End Function

    End Class
End Namespace
